package com.subang.function.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.subang.common.annotation.Log;
import com.subang.common.core.controller.BaseController;
import com.subang.common.core.domain.AjaxResult;
import com.subang.common.enums.BusinessType;
import com.subang.function.domain.SbPkRoom;
import com.subang.function.service.ISbPkRoomService;
import com.subang.common.utils.poi.ExcelUtil;
import com.subang.common.core.page.TableDataInfo;

/**
 * PK答题房间信息Controller
 * 
 * @author subei
 * @date 2025-09-14
 */
@RestController
@RequestMapping("/function/pkRoom")
public class SbPkRoomController extends BaseController
{
    @Autowired
    private ISbPkRoomService sbPkRoomService;

    /**
     * 查询PK答题房间信息列表
     */
    @PreAuthorize("@ss.hasPermi('function:room:list')")
    @GetMapping("/list")
    public TableDataInfo list(SbPkRoom sbPkRoom)
    {
        startPage();
        List<SbPkRoom> list = sbPkRoomService.selectSbPkRoomList(sbPkRoom);
        return getDataTable(list);
    }

    /**
     * 导出PK答题房间信息列表
     */
    @PreAuthorize("@ss.hasPermi('function:room:export')")
    @Log(title = "PK答题房间信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SbPkRoom sbPkRoom)
    {
        List<SbPkRoom> list = sbPkRoomService.selectSbPkRoomList(sbPkRoom);
        ExcelUtil<SbPkRoom> util = new ExcelUtil<SbPkRoom>(SbPkRoom.class);
        util.exportExcel(response, list, "PK答题房间信息数据");
    }

    /**
     * 获取PK答题房间信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sbPkRoomService.selectSbPkRoomById(id));
    }

    /**
     * 新增PK答题房间信息
     */
    @Log(title = "PK答题房间信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SbPkRoom sbPkRoom)
    {
        int result = sbPkRoomService.insertSbPkRoom(sbPkRoom);
        if (result > 0)
        {
            // 查询刚刚创建的房间信息
            SbPkRoom newRoom = sbPkRoomService.selectSbPkRoomById(sbPkRoom.getId());
            if (newRoom != null)
            {
                // 返回房间id和房间代码
                return success().put("roomId", newRoom.getId()).put("roomCode", newRoom.getRoomCode());
            }
        }
        return toAjax(result);
    }

    /**
     * 修改PK答题房间信息
     */
    @PreAuthorize("@ss.hasPermi('function:room:edit')")
    @Log(title = "PK答题房间信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SbPkRoom sbPkRoom)
    {
        return toAjax(sbPkRoomService.updateSbPkRoom(sbPkRoom));
    }

    /**
     * 删除PK答题房间信息
     */
    @PreAuthorize("@ss.hasPermi('function:room:remove')")
    @Log(title = "PK答题房间信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sbPkRoomService.deleteSbPkRoomByIds(ids));
    }
}
